package com.csun.dsms.service.impl;

import com.csun.dsms.comment.base.enums.ErrorCodeEnum;
import com.csun.dsms.comment.base.exception.BusinessException;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.DsmsRecordMapper;
import com.csun.dsms.model.domain.*;
import com.csun.dsms.model.dto.healthReport.HealthReportDTO;
import com.csun.dsms.model.dto.record.FinishTaskDto;
import com.csun.dsms.model.dto.record.RecordIdsAndEvaluation;
import com.csun.dsms.model.dto.record.RecordPercentDto;
import com.csun.dsms.model.dto.record.RecordQueryDto;
import com.csun.dsms.model.vo.poor.PoorProjectVo;
import com.csun.dsms.model.vo.poor.PoorVo;
import com.csun.dsms.model.vo.project.ProjectVo;
import com.csun.dsms.model.vo.record.ProjectPercentVo;
import com.csun.dsms.model.vo.record.RecordPercentVo;
import com.csun.dsms.model.vo.staff.StaffVo;
import com.csun.dsms.service.*;
import com.csun.dsms.utils.DateUtil;
import com.csun.dsms.utils.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-26 11:53
 * @description: TODO
 */
@Slf4j
@Service
@Transactional
public class DsmsRecordServiceImpl extends BaseService<DsmsRecord> implements DsmsRecordService {

    @Resource
    private DsmsRecordMapper dsmsRecordMapper;
    @Resource
    private DsmsPoorProjectService dsmsPoorProjectService;
    @Resource
    private DsmsPoorService dsmsPoorService;
    @Resource
    private DsmsStaffService dsmsStaffService;
    @Resource
    private DsmsFeeService dsmsFeeService;
    @Resource
    private DsmsFeeRecordService dsmsFeeRecordService;
    @Resource
    private DsmsHealthReportService healthReportService;
    @Resource
    private DsmsRecordProjectService dsmsRecordProjectService;

    /**
     * 查询服务记录列表 - 分页.
     *
     * @param recordQueryDto    the record query dto
     *
     * @return  the dsms record list.
     */
    @Override
    public List<DsmsRecord> queryListWithPage(RecordQueryDto recordQueryDto) {

        PageHelper.startPage(recordQueryDto.getPageNum(), recordQueryDto.getPageSize());

        return dsmsRecordMapper.queryListWithPage(recordQueryDto);
    }

    /**
     * 自动生成服务记录
     *
     * @return  the int.
     */
    @Override
    public Integer autoCreate(String date) {

        // 查询当月没有的服务记录的特困人员信息
        List<PoorVo> poorVoList = dsmsPoorProjectService.queryNotExitRecordList(date);

        int days = DateUtil.getDaysOfMonth(new Date());
        boolean flag = true;    // 一天或多天服务一次
        int times = 0;

        List<DsmsRecord> dsmsRecordList = Lists.newArrayList();
        for (PoorVo pv : poorVoList) {
            if (PublicUtil.isEmpty(pv.getAreaId())) {
                log.error("the area is null");
                continue;
            }
            int s_day = 0;
            int e_day = 0;
            if(pv.getCount() != null) {
                for (int i = 0; i < pv.getCount(); i++) {
                    if (days >= pv.getCount()) { // 一天或多天服务一次
                        flag = true;
                        times = days / pv.getCount();
                        if(times < 2){
                            times =0;
                        }
                    } else {    // 一天内服务多次
                        flag = false;
                        times = 24 / days;
                    }
                        DsmsRecord dsmsRecord = new DsmsRecord();
                        dsmsRecord.setPoorId(pv.getId());       // 特困人员id
                        dsmsRecord.setPoorName(pv.getName());   // 特困人员姓名
                        dsmsRecord.setPoorHeadPic(pv.getHeadPic()); // 特困人员头像
                        dsmsRecord.setPoorAge(pv.getAge());     // 特困人员姓名
                        dsmsRecord.setUnitId(pv.getUnitId());   // 特困人员单位id
                        dsmsRecord.setUnitName(pv.getUnitName());   // 特困人员单位名称（居住地）
                        dsmsRecord.setSelfCareAbility(pv.getSelfCareAbility());  // 服务项目自理能力
                        dsmsRecord.setAreaId(pv.getAreaId());       // 网格id
                        dsmsRecord.setAreaName(pv.getAreaName());   // 网格名称
                        dsmsRecord.setCompanyId(pv.getCompanyId()); // 服务公司Id
                        dsmsRecord.setCompanyName(pv.getCompanyName()); // 服务公司名称
                        String start = "";
                        String end = "";
                        if (flag) {
    //                            int s_day = (i - 1) * times + 1;
    //                            int e_day = s_day + times;
                            s_day = e_day+1;
                            e_day = s_day + times;
                            // 判断是不是超过了最后一天
                            if (e_day > days) {
                                e_day = days;
                            }
                            if (s_day < 10) {
                                start = date + "-0" + s_day + " 00:00:00";
                            } else {
                                start = date + "-" + s_day + " 00:00:00";
                            }
                            if (e_day < 10) {
                                end = date + "-0" + e_day + " 23:59:59";
                            } else {
                                end = date + "-" + e_day + " 23:59:59";
                            }
                        } else {
//                            int day = (i  + 1) / times;
//                            String str_day = "";
//                            if (day < 10) {
//                                str_day = date + "-0" + day;
//                            } else {
//                                str_day = date + "-" + day;
//                            }
//
//                            int day_times = i - ((day - 1) * times);
//
//                            int s_hour = (day_times - 1) * (24 / times);
//                            int e_hour = day_times * (24 / times) - 1;
//
//                            if (s_hour < 10) {
//                                start = str_day + " 0" + s_hour + ":00:00";
//                            } else {
//                                start = date + " " + s_hour + " 00:00:00";
//                            }
//                            if (e_hour < 10) {
//                                end = str_day + " 0" + e_hour + ":59:59";
//                            } else {
//                                end = str_day + " " + e_hour + ":59:59";
//                            }
                    }
                    dsmsRecord.setServiceStartTime(start);
                    dsmsRecord.setServiceEndTime(end);
                    dsmsRecordList.add(dsmsRecord);


                }
            }
            /*for (PoorProjectVo pp : pv.getProjectVoList()) {
                if (PublicUtil.isNotEmpty(pp.getProjectFrequency())
                    && pp.getProjectFrequency() > 0) {
                    if (days >= pp.getProjectFrequency()) { // 一天或多天服务一次
                        flag = true;
                        times = days / pp.getProjectFrequency();
                    } else {    // 一天内服务多次
                        flag = false;
                        times = 24 / days;
                    }
                    int s_day = 0;
                    int e_day = 0;
                    for (int i = 1; i <= pp.getProjectFrequency(); i++) {
                        DsmsRecord dsmsRecord = new DsmsRecord();
                        dsmsRecord.setPoorId(pv.getId());       // 特困人员id
                        dsmsRecord.setPoorName(pv.getName());   // 特困人员姓名
                        dsmsRecord.setPoorHeadPic(pv.getHeadPic()); // 特困人员头像
                        dsmsRecord.setPoorAge(pv.getAge());     // 特困人员姓名
                        dsmsRecord.setUnitId(pv.getUnitId());   // 特困人员单位id
                        dsmsRecord.setUnitName(pv.getUnitName());   // 特困人员单位名称（居住地）
                        dsmsRecord.setProjectId(pp.getProjectId()); // 项目id
                        dsmsRecord.setProjectName(pp.getProjectName()); // 项目名称
                        dsmsRecord.setSelfCareAbility(pp.getProjectSelfCareAbility());  // 服务项目自理能力
                        dsmsRecord.setServicePrice(pp.getProjectPrice());   // 项目价格
                        dsmsRecord.setAreaId(pv.getAreaId());       // 网格id
                        dsmsRecord.setAreaName(pv.getAreaName());   // 网格名称
                        dsmsRecord.setCompanyId(pv.getCompanyId()); // 服务公司Id
                        dsmsRecord.setCompanyName(pv.getCompanyName()); // 服务公司名称
                        dsmsRecord.setServiceDuration(pp.getServiceDuration());//服务时长
                        String start = "";
                        String end = "";
                        if (flag) {
//                            int s_day = (i - 1) * times + 1;
//                            int e_day = s_day + times;
                            s_day = e_day + 1;
                            e_day = s_day + times;
                            // 判断是不是超过了最后一天
                            if (e_day > days) {
                                e_day = days;
                            }
                            if (s_day < 10) {
                                start = date + "-0" + s_day + " 00:00:00";
                            } else {
                                start = date + "-" + s_day + " 00:00:00";
                            }
                            if (e_day < 10) {
                                end = date + "-0" + e_day + " 23:59:59";
                            } else {
                                end = date + "-" + e_day + " 23:59:59";
                            }
                        } else {
//                            int day = (i  + 1) / times;
//                            String str_day = "";
//                            if (day < 10) {
//                                str_day = date + "-0" + day;
//                            } else {
//                                str_day = date + "-" + day;
//                            }
//
//                            int day_times = i - ((day - 1) * times);
//
//                            int s_hour = (day_times - 1) * (24 / times);
//                            int e_hour = day_times * (24 / times) - 1;
//
//                            if (s_hour < 10) {
//                                start = str_day + " 0" + s_hour + ":00:00";
//                            } else {
//                                start = date + " " + s_hour + " 00:00:00";
//                            }
//                            if (e_hour < 10) {
//                                end = str_day + " 0" + e_hour + ":59:59";
//                            } else {
//                                end = str_day + " " + e_hour + ":59:59";
//                            }
                        }
                        dsmsRecord.setServiceStartTime(start);
                        dsmsRecord.setServiceEndTime(end);

                        dsmsRecordList.add(dsmsRecord);
                    }
                }
            }*/
        }

        int result = batchSave(dsmsRecordList);

        dsmsRecordMapper.dealTimeOut();

        return result;
    }

    /**
     * 完成任务
     *
     * @param finishTaskDto
     * @param login
     *
     * @return
     */
    @Override
    @Transactional
    public Integer finishTask(FinishTaskDto finishTaskDto, DsmsUser login) {
        // 查询用户是否为服务人员
        StaffVo staffVo = dsmsStaffService.selectByUserId(login.getId());
        if (PublicUtil.isEmpty(staffVo)) {
            throw new BusinessException(ErrorCodeEnum.GB10051001);
        }
        // 校验任务是否存在
        //DsmsRecord dsmsRecordQuery = dsmsRecordMapper.selectByPrimaryKey(finishTaskDto.getId());
        List<DsmsRecord> dsmsRecords = dsmsRecordMapper.selectByRecordIds(finishTaskDto.getIds());
        Integer result = 0;
        for (DsmsRecord dsmsRecordQuery:dsmsRecords) {
            if (PublicUtil.isEmpty(dsmsRecordQuery)) {
                throw new BusinessException(ErrorCodeEnum.GB10061001);
            }

            // 校验任务是否待处理
            if (!dsmsRecordQuery.getStatus().equals(5)) {
                throw new BusinessException(ErrorCodeEnum.GB10061002);
            }
            // 更新任务服务信息
            DsmsRecord dsmsRecord = new DsmsRecord();
            dsmsRecord.setId(dsmsRecordQuery.getId());
            dsmsRecord.setUpdateInfo(login);/*
            dsmsRecord.setStaffId(staffVo.getId());
            dsmsRecord.setStaffName(login.getUserName());
            dsmsRecord.setCompanyId(staffVo.getUnitId());
            dsmsRecord.setCompanyName(staffVo.getUnitName());*/
            dsmsRecord.setRemark(finishTaskDto.getRemark());
            List<DsmsRecordProject> projects = new ArrayList<>();
            DsmsRecordProject dsmsRecordProject = new DsmsRecordProject();
            for (ProjectVo project:finishTaskDto.getProjectVos()) {
                dsmsRecordProject.setRecordId(dsmsRecord.getId());
                dsmsRecordProject.setProjectId(project.getProjectId());
                dsmsRecordProject.setProjectName(project.getProjectName());
                projects.add(dsmsRecordProject);
                dsmsRecordProject = new DsmsRecordProject();
            }
            dsmsRecordProjectService.batchSave(projects);
            //判断是否为多人任务
            if(finishTaskDto.getOtherStaffName() != null && !finishTaskDto.getOtherStaffName().equals("")){
                dsmsRecord.setIsMany(1);
                dsmsRecord.setOtherStaffName(finishTaskDto.getOtherStaffName());
            }else {
                dsmsRecord.setIsMany(0);
            }

            // 服务信息
            //dsmsRecord.setServicePicBefore(finishTaskDto.getServicePicBefore());
            dsmsRecord.setServicePicAfter(finishTaskDto.getServicePicAfter());
            if (PublicUtil.isEmpty(finishTaskDto.getServiceEvaluate())) {
                dsmsRecord.setRealAmount(dsmsRecordQuery.getServicePrice()
                        .multiply(new BigDecimal(0.8)).setScale(2, BigDecimal.ROUND_HALF_UP));
            } else {
                dsmsRecord.setServiceEvaluate(finishTaskDto.getServiceEvaluate());
                dsmsRecord.setRealAmount(dsmsRecordQuery.getServicePrice());
            }

            // 处理信息
            dsmsRecord.setStatus(2);    // 已办理
            dsmsRecord.setServiceTime(DateUtil.getNowTime());
            dsmsRecord.setEndServiceTime(DateUtil.getNowTime());
            try{
                Date parse = DateUtil.YYYY_MM_DD_MM_HH_SS.parse(dsmsRecord.getStartServiceTime());
                long l = (new Date().getTime() - parse.getTime()) / (1000 * 60 * 60);
                BigDecimal bigDecimal = new BigDecimal(l);
                dsmsRecord.setServiceDuration(bigDecimal);
            }catch (Exception e){

            }
            DsmsStaff staff = new DsmsStaff();
            staff.setId(staffVo.getId());
            staff.setServiceTimes(staffVo.getServiceTimes()+1);
            dsmsStaffService.update(staff);
            Integer i = dsmsRecordMapper.updateByPrimaryKeySelective(dsmsRecord);
            result = result +i;
            if (i > 0) {
                // 插入费用记录
                // 根据月份, 网格id查询
                DsmsFee dsmsFee = new DsmsFee();
                dsmsFee.setAreaId(dsmsRecordQuery.getAreaId());
                dsmsFee.setMount(DateUtil.getNowMonth());
                dsmsFee = dsmsFeeService.selectOne(dsmsFee);
                if (PublicUtil.isEmpty(dsmsFee)) {
                    dsmsFee = new DsmsFee();
                    dsmsFee.setAreaId(dsmsRecordQuery.getAreaId());
                    dsmsFee.setAreaName(dsmsRecordQuery.getAreaName());
                    dsmsFee.setMount(DateUtil.getNowMonth());
                    dsmsFee.setUnitId(staffVo.getUnitId());     // 公司
                    dsmsFee.setUnitName(staffVo.getUnitName());
                    dsmsFee.setAmount(dsmsRecord.getRealAmount());
                    dsmsFee.setUpdateInfo(login);
                    dsmsFeeService.save(dsmsFee);
                } else {
                    dsmsFee.setAmount(dsmsFee.getAmount().add(dsmsRecord.getRealAmount()));
                    dsmsFee.setUpdateInfo(login);
                    dsmsFeeService.update(dsmsFee);
                }
                DsmsFeeRecord dsmsFeeRecord = new DsmsFeeRecord();
                dsmsFeeRecord.setFeeId(dsmsFee.getId());
                dsmsFeeRecord.setRecordId(dsmsRecordQuery.getId());
                dsmsFeeRecordService.save(dsmsFeeRecord);
            }
        }
        if(finishTaskDto.getHealthReportDTO() != null){
            DsmsHealthReport healthReport = new DsmsHealthReport();
            healthReport.setPoorId(finishTaskDto.getHealthReportDTO().getPoorId());
            healthReport.setCreateTime(new Date());
            healthReport.setBloodPressure(finishTaskDto.getHealthReportDTO().getBloodPressure());
            healthReport.setBloodSugar(finishTaskDto.getHealthReportDTO().getBloodSugar());
            healthReport.setRemark(finishTaskDto.getHealthReportDTO().getRemark());
            healthReportService.save(healthReport);
        }
        /*if (PublicUtil.isEmpty(dsmsRecordQuery)) {
            throw new BusinessException(ErrorCodeEnum.GB10061001);
        }

        // 校验任务是否待处理
        if (!dsmsRecordQuery.getStatus().equals(1)) {
            throw new BusinessException(ErrorCodeEnum.GB10061002);
        }*/

        // 更新任务服务信息
        /*DsmsRecord dsmsRecord = new DsmsRecord();
        dsmsRecord.setId(finishTaskDto.getId());
        dsmsRecord.setUpdateInfo(login);
        dsmsRecord.setStaffId(staffVo.getId());
        dsmsRecord.setStaffName(login.getUserName());
        dsmsRecord.setCompanyId(staffVo.getUnitId());
        dsmsRecord.setCompanyName(staffVo.getUnitName());
        dsmsRecord.setRemark(finishTaskDto.getRemark());
        //判断是否为多人任务
        if(finishTaskDto.getOtherStaffName() != null && !finishTaskDto.getOtherStaffName().equals("")){
            dsmsRecord.setIsMany(1);
            dsmsRecord.setOtherStaffName(finishTaskDto.getOtherStaffName());
        }else {
            dsmsRecord.setIsMany(0);
        }

        // 服务信息
        dsmsRecord.setServicePicBefore(finishTaskDto.getServicePicBefore());
        dsmsRecord.setServicePicAfter(finishTaskDto.getServicePicAfter());
        if (PublicUtil.isEmpty(finishTaskDto.getServiceEvaluate())) {
            dsmsRecord.setRealAmount(dsmsRecordQuery.getServicePrice()
                    .multiply(new BigDecimal(0.8)).setScale(2, BigDecimal.ROUND_HALF_UP));
        } else {
            dsmsRecord.setServiceEvaluate(finishTaskDto.getServiceEvaluate());
            dsmsRecord.setRealAmount(dsmsRecordQuery.getServicePrice());
        }

        // 处理信息
        dsmsRecord.setStatus(2);    // 已办理
        dsmsRecord.setServiceTime(DateUtil.getNowTime());

        Integer result = dsmsRecordMapper.updateByPrimaryKeySelective(dsmsRecord);
        if (result > 0) {
            // 插入费用记录
            // 根据月份, 网格id查询
            DsmsFee dsmsFee = new DsmsFee();
            dsmsFee.setAreaId(dsmsRecordQuery.getAreaId());
            dsmsFee.setMount(DateUtil.getNowMonth());
            dsmsFee = dsmsFeeService.selectOne(dsmsFee);
            if (PublicUtil.isEmpty(dsmsFee)) {
                dsmsFee = new DsmsFee();
                dsmsFee.setAreaId(dsmsRecordQuery.getAreaId());
                dsmsFee.setAreaName(dsmsRecordQuery.getAreaName());
                dsmsFee.setMount(DateUtil.getNowMonth());
                dsmsFee.setUnitId(staffVo.getUnitId());     // 公司
                dsmsFee.setUnitName(staffVo.getUnitName());
                dsmsFee.setAmount(dsmsRecord.getRealAmount());
                dsmsFee.setUpdateInfo(login);
                dsmsFeeService.save(dsmsFee);
            } else {
                dsmsFee.setAmount(dsmsFee.getAmount().add(dsmsRecord.getRealAmount()));
                dsmsFee.setUpdateInfo(login);
                dsmsFeeService.update(dsmsFee);
            }
            DsmsFeeRecord dsmsFeeRecord = new DsmsFeeRecord();
            dsmsFeeRecord.setFeeId(dsmsFee.getId());
            dsmsFeeRecord.setRecordId(dsmsRecordQuery.getId());
            dsmsFeeRecordService.save(dsmsFeeRecord);
        }*/
        return result;
    }

    /**
     * 查询服务进度
     *
     * @param recordPercentDto  the record percent dto
     *
     * @return  the record percent vo list.
     */
    @Override
    public List<RecordPercentVo> queryRecordPercent(RecordPercentDto recordPercentDto) {

        return dsmsRecordMapper.queryRecordPercent(recordPercentDto);
    }

    /**
     * 查询项目进度
     *
     * @param id    the poor id
     * @param date  the date
     *
     * @return  the project percent vo list.
     */
    @Override
    public List<ProjectPercentVo> queryProjectPercent(Long id, String date) {

        return dsmsRecordMapper.queryProjectPercent(id, date);
    }

    /**
     * 根据id取消服务任务
     *
     * @param id    the record id
     * @param login the login user
     *
     * @return
     */
    @Override
    public Integer cancelById(Long id, DsmsUser login) {

        // 校验任务是否存在
        DsmsRecord dsmsRecordQuery = dsmsRecordMapper.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(dsmsRecordQuery)) {
            throw new BusinessException(ErrorCodeEnum.GB10061001);
        }

        // 校验任务是否待办
        if (!dsmsRecordQuery.getStatus().equals(1)) {
            throw new BusinessException(ErrorCodeEnum.GB10061003);
        }

        DsmsRecord dsmsRecord = new DsmsRecord();
        dsmsRecord.setId(id);
        dsmsRecord.setUpdateInfo(login);
        dsmsRecord.setStatus(4);    // 更新为已逾期

        return dsmsRecordMapper.updateByPrimaryKeySelective(dsmsRecord);
    }

    /**
     * 更新服务记录特困人员头像照片
     *
     * @param poorId    the poor id
     * @param headPic   the poor head pic
     *
     * @return  the int.
     */
    @Override
    public Integer updatePoorHeadPic(Long poorId, String headPic) {

        return dsmsRecordMapper.updatePoorHeadPic(poorId, headPic);
    }
    //查询临时任务
    @Override
    public List<DsmsRecord> queryListByType(RecordQueryDto recordQueryDto) {
        return dsmsRecordMapper.queryListByType(recordQueryDto);
    }

    //服务评价
    @Override
    public Integer approvalRecordBycompany(RecordIdsAndEvaluation recordIdsAndEvaluation) {
        return dsmsRecordMapper.approvalRecordBycompany(recordIdsAndEvaluation);
    }

    //判断评价的记录是否已完成
    @Override
    public List<DsmsRecord> selectByRecordIds(List<Long> ids) {
        return dsmsRecordMapper.selectByRecordIds(ids);
    }

    @Override
    public Integer queryServices(RecordQueryDto recordQueryDto) {
        return dsmsRecordMapper.queryServices(recordQueryDto);
    }

    //点击开始任务
    @Override
    public Integer startTask(FinishTaskDto finishTaskDto, DsmsUser login) {
        // 查询用户是否为服务人员
        StaffVo staffVo = dsmsStaffService.selectByUserId(login.getId());
        if (PublicUtil.isEmpty(staffVo)) {
            throw new BusinessException(ErrorCodeEnum.GB10051001);
        }
        // 校验任务是否存在
        //DsmsRecord dsmsRecordQuery = dsmsRecordMapper.selectByPrimaryKey(finishTaskDto.getId());
        List<DsmsRecord> dsmsRecords = dsmsRecordMapper.selectByRecordIds(finishTaskDto.getIds());
        Integer result = 0;
        for (DsmsRecord dsmsRecordQuery:dsmsRecords) {
            if (PublicUtil.isEmpty(dsmsRecordQuery)) {
                throw new BusinessException(ErrorCodeEnum.GB10061001);
            }

            // 校验任务是否待处理
            if (!dsmsRecordQuery.getStatus().equals(1)) {
                throw new BusinessException(ErrorCodeEnum.GB10061002);
            }
            // 更新任务服务信息
            DsmsRecord dsmsRecord = new DsmsRecord();
            dsmsRecord.setId(dsmsRecordQuery.getId());
            dsmsRecord.setUpdateInfo(login);
            dsmsRecord.setStaffId(staffVo.getId());
            dsmsRecord.setStaffName(login.getUserName());
            dsmsRecord.setCompanyId(staffVo.getUnitId());
            dsmsRecord.setCompanyName(staffVo.getUnitName());
            dsmsRecord.setRemark(finishTaskDto.getRemark());
            dsmsRecord.setServicePicBefore(finishTaskDto.getServicePicBefore());
            dsmsRecord.setStatus(5);
            dsmsRecord.setStartServiceTime(DateUtil.getNowTime());
            Integer i = dsmsRecordMapper.updateByPrimaryKeySelective(dsmsRecord);
            result = result +i;
        }
        return result;
    }

    //任务进行中上传图片
    @Override
    public Integer ongoingTask(FinishTaskDto finishTaskDto, DsmsUser login) {

        // 校验任务是否存在
        //DsmsRecord dsmsRecordQuery = dsmsRecordMapper.selectByPrimaryKey(finishTaskDto.getId());
        List<DsmsRecord> dsmsRecords = dsmsRecordMapper.selectByRecordIds(finishTaskDto.getIds());
        Integer result = 0;
        for (DsmsRecord dsmsRecordQuery:dsmsRecords) {
            if (PublicUtil.isEmpty(dsmsRecordQuery)) {
                throw new BusinessException(ErrorCodeEnum.GB10061001);
            }

            // 校验任务是否待处理
            if (!dsmsRecordQuery.getStatus().equals(5)) {
                throw new BusinessException(ErrorCodeEnum.GB10061002);
            }
            // 更新任务服务信息
            DsmsRecord dsmsRecord = new DsmsRecord();
            dsmsRecord.setId(dsmsRecordQuery.getId());
            dsmsRecord.setUpdateInfo(login);
            dsmsRecord.setRemark(finishTaskDto.getRemark());
            dsmsRecord.setServicePicAfter(finishTaskDto.getServicePicAfter());
            Integer i = dsmsRecordMapper.updateByPrimaryKeySelective(dsmsRecord);
            result = result +i;
        }
        return result;
    }
}
